package com.zy.imagineCommunity.service;

import com.zy.imagineCommunity.dao.BannerMapper;
import com.zy.imagineCommunity.entity.Banner;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.entity.UserText;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BannerService {

    @Autowired
    BannerMapper bannerMapper;

    /**
     * 轮播图集合
     *
     * @return
     */
    public List<Banner> queryBannerList() {
        return bannerMapper.queryBannerList();
    }

    /**
     * 根据页码、每页个数查询轮播图
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Banner> queryBannerByPageNoAndPageSize(int pageNo, int pageSize) {
        Page<Banner> page = new Page<Banner>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页个数
        page.setPageSize(pageSize);
        //设置总记录数
        int pageCountTotal = bannerMapper.queryBannerTotalCount();
        page.setPageCountTotal(pageCountTotal);
        //设置总页码
        int pageTotal = pageCountTotal / pageSize;
        if (pageCountTotal % pageSize > 0) {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //计算起始条数，查询内容
        int begin = (pageNo - 1) * pageSize;
        List<Banner> bannerList = bannerMapper.queryBannerByBeginAndPageSize(begin, pageSize);
        //设置每页内容
        page.setItems(bannerList);
        return page;
    }

    public void deleteBanner(int id) {
        bannerMapper.deleteBanner(id);
    }

    public void addBanner(Banner banner) {
        bannerMapper.addBanner(banner);
    }
}
